﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;

namespace ebMS3dotnet.Core
{
    class ebMS3PackagingChannelBase<TShape>:ChannelBase
        where TShape:class, IChannel
    {
        private TShape _innerChannel;
        private ebMS3Context _context = new ebMS3Context();

        protected ebMS3PackagingChannelBase(ChannelManagerBase channelManager,
            TShape innerChannel)
            : base(channelManager)
        {
            if (innerChannel == null)
            {
                throw new ArgumentNullException("ebMS3PackagingChannelBase requires a non-null inner channel.", "innerChannel");
            }
            _innerChannel = innerChannel;
        }

        protected TShape InnerChannel
        {
            get { return _innerChannel; }
        }

        public override T GetProperty<T>() {
            if (typeof(T) == typeof(ebMS3Context))
                return (T)(object)_context;
            return _innerChannel.GetProperty<T>(); 
        }

        protected override void OnAbort()
        {
            _innerChannel.Abort();
        }

        protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
        {
            return _innerChannel.BeginClose(timeout, callback, state);
        }

        protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
        {
            return _innerChannel.BeginOpen(timeout, callback, state);
        }

        protected override void OnClose(TimeSpan timeout)
        {
            _innerChannel.Close(timeout);
        }

        protected override void OnEndClose(IAsyncResult result)
        {
            _innerChannel.EndClose(result);
        }

        protected override void OnEndOpen(IAsyncResult result)
        {
            _innerChannel.EndOpen(result);
        }

        protected override void OnOpen(TimeSpan timeout)
        {
            _innerChannel.Open(timeout);
        }
    }
}
